import { forEach, findIndex, find } from "lodash";
export const handleTopoData = (apiData, activeItem) => {
  const nodes = [],
    edges = [];
  forEach(apiData, (data) => {
    if (findIndex(nodes, { id: data.srcWorkloadKey }) === -1) {
      nodes.push({
        id: data.srcWorkloadKey,
        workloadName: data.srcWorkloadName,
        workloadKind: data.srcWorkloadKind,
        namespace: data.srcNamespace,
        masterIp: data.srcMasterip.key,
        clusterName: data.srcMasterip.value,
        ip: data.srcIp,
        port: data.srcPort,
      });
    }
    if (findIndex(nodes, { id: data.dstWorkloadKey }) === -1) {
      nodes.push({
        id: data.dstWorkloadKey,
        workloadName: data.dstWorkloadName,
        workloadKind: data.dstWorkloadKind,
        namespace: data.dstNamespace,
        masterIp: data.dstMasterip.key,
        clusterName: data.dstMasterip.value,
        ip: data.dstIp,
        port: data.dstPort,
      });
    }
    if (
      findIndex(edges, {
        source: data.srcWorkloadKey,
        target: data.dstWorkloadKey,
      }) > -1
    ) {
      const nowEdge = find(edges, {
        source: data.srcWorkloadKey,
        target: data.dstWorkloadKey,
      });
      nowEdge.urlList = [...nowEdge.urlList, data];
    } else {
      edges.push({
        ...data,
        source: data.srcWorkloadKey,
        target: data.dstWorkloadKey,
        isNowRequest:
          data.srcWorkloadKey === activeItem.srcWorkloadKey &&
          data.dstWorkloadKey === activeItem.dstWorkloadKey,
        urlList: [data],
      });
    }
  });
  return { nodes, edges };
};
// 拓扑图搜索节点focus
export const graphFocusNodeNew = (item, graph, isFocus = false) => {
  const allNodes = graph.getNodes();
  const allEdges = graph.getEdges();
  const allCombos = graph.getCombos();
  // ! 折叠起来的combo只能通过combo来查edge
  const allEdgesPlus = allCombos.reduce((prev, cur) => {
      prev.push(...cur.getEdges());
      return prev;
  }, []);

  if (item) {
      const nodeId = item.getModel().id;

      [].concat(allEdgesPlus, allNodes, allEdges).forEach((item) => {
          const group = item.getContainer();
          const children = group.get("children");
          children.forEach((item) => {
              item.attr({
                  opacity: 0.3,
              });
          });
      });
      // * 获取关联的边
      const relatedEdges = item.getEdges();
      //* 根据边找到所有的关联节点和边
      // ! 通过边来查找关系,会导致一个问题,没有边的节点,查不到任何节点和边
      const sourceNodes = relatedEdges.map(
          (item) => item.getSource().getModel().id
      );
      const targetNodes = relatedEdges.map(
          (item) => item.getTarget().getModel().id
      );
      const edges = relatedEdges.map((item) => item.getModel().id);
      // * 去重之后高亮
      Array.from(
          new Set([].concat(sourceNodes, targetNodes, edges, nodeId))
      ).forEach((id) => {
          const graphItem = graph.findById(id);
          const itemChildren = graphItem.get("group").get("children");
          itemChildren.forEach((item) => {
              item.attr({ opacity: 1 });
          });
      });

      isFocus && graph.focusItem(item);
  } else {
      [].concat(allNodes, allEdges, allEdgesPlus).forEach((item) => {
          const group = item.getContainer();
          const children = group.get("children");
          children.forEach((item) => {
              item.attr({
                  opacity: 1,
              });
          });
      });
  }
};
